using System;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using Orchard.Environment;
using Orchard.Environment.Extensions;
using Orchard.UI.Resources;
using EMS.HTMLKickStart.Models;
using EMS.HTMLKickStart.Services;
using System.Collections.Generic;
using Orchard.Localization;

namespace EMS.HTMLKickStart.Handlers {
    public class HTMLKickStartSettingsPartHandler : ContentHandler {
        private readonly Work<IResourceManager> _resourceManager;
        private readonly IHTMLKickStartService _htmlKickStartService;

        public HTMLKickStartSettingsPartHandler(IRepository<HTMLKickStartSettingsPartRecord> repository, 
            IHTMLKickStartService htmlKickStartService,
            Work<IResourceManager> resourceManager) {
            T = NullLocalizer.Instance;
            Filters.Add(StorageFilter.For(repository));
            Filters.Add(new ActivatingFilter<HTMLKickStartSettingsPart>("Site"));
            _htmlKickStartService = htmlKickStartService;
            _resourceManager = resourceManager;
        }

        public Localizer T { get; set; }

        protected override void GetItemMetadata(GetContentItemMetadataContext context) {
            if (context.ContentItem.ContentType != "Site")
                return;
            base.GetItemMetadata(context);
            // Add in the menu option for the Settings
            context.Metadata.EditorGroupInfo.Add(new GroupInfo(T("HTML KickStart")));
        }
        
        protected override void BuildDisplayShape(BuildDisplayContext context) {
            if (context.ContentItem.ContentType == "Page") {
                if (_htmlKickStartService.GetIsIncludeHTMLKickStartEveryPage()) {
                    // Add Styles
                    var htmlKickStartStyles = GetHTMLKickStartStyles();
                    foreach (var htmlKickStartStyle in htmlKickStartStyles) {
                        _resourceManager.Value.Require("stylesheet", htmlKickStartStyle);
                    }
                    // Add Scripts
                    var htmlKickStartScripts = GetHTMLKickStartScripts();
                    foreach (var htmlKickStartScript in htmlKickStartScripts) {
                        _resourceManager.Value.Require("script", htmlKickStartScript).AtHead();
                    }
                }
            }
            base.BuildDisplayShape(context);
        }

        private List<string> GetHTMLKickStartStyles() {
            List<string> htmlKickStartStyles = new List<string>();
            
            // Just need to add "KickStart" as this references the remaining CSS stylesheets
            htmlKickStartStyles.Add("KickStart");
            
            return htmlKickStartStyles;
        }

        private List<string> GetHTMLKickStartScripts() {
            List<string> htmlKickStartScripts = new List<string>();

            htmlKickStartScripts.Add("KickStart");
            htmlKickStartScripts.Add("Prettify");

            return htmlKickStartScripts;
        }
    }
}
